package cn.mulanbay.job.web.controller;

import cn.mulanbay.business.util.TreeBeanUtil;
import cn.mulanbay.common.bean.ResultBean;
import cn.mulanbay.common.bean.TreeBean;
import cn.mulanbay.common.exception.ApplicationException;
import cn.mulanbay.common.exception.ErrorCode;
import cn.mulanbay.job.persistent.service.TaskServerService;
import cn.mulanbay.job.web.bean.request.server.TaskServerSearch;
import cn.mulanbay.schedule.domain.TaskServer;
import cn.mulanbay.web.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 调度服务器
 *
 * @author fenghong
 * @create 2022-01-16 10:00
 */
@RestController
@RequestMapping("/taskServer")
public class TaskServerController extends BaseController {

    @Autowired
    TaskServerService taskServerService;

    /**
     * 获取服务器树
     * @return
     */
    @RequestMapping(value = "/getTaskServerTree")
    public ResultBean getTaskServerTree(Boolean needRoot) {
        try {
            List<TreeBean> list = new ArrayList<TreeBean>();
            List<TaskServer> gtList = taskServerService.list();
            for (TaskServer gt : gtList) {
                TreeBean tb = new TreeBean();
                tb.setId(gt.getDeployId());
                tb.setText(gt.getDeployId());
                list.add(tb);
            }
            return callback(TreeBeanUtil.addRoot(list, needRoot));
        } catch (Exception e) {
            throw new ApplicationException(ErrorCode.SYSTEM_ERROR, "获取服务器树异常",
                    e);
        }
    }

    /**
     * 获取列表数据
     * @param sf
     * @return
     */
    @RequestMapping(value = "/getData", method = RequestMethod.GET)
    public ResultBean getData(TaskServerSearch sf) {
        return callbackDataGrid(taskServerService.getDataList(sf));
    }
}
